package com.bignerdranch.android.furnitureapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.bignerdranch.android.furnitureapplication.adapter.RedKnowAdapter;
import com.bignerdranch.android.furnitureapplication.entity.RedWoodInfoBean;
import com.bignerdranch.android.furnitureapplication.net.RetrofitHelper;

import java.util.List;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class RedWoodKnowActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView;
    private List<RedWoodInfoBean> dataList;
    private ImageView backImage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_red_wood_know);

        mRecyclerView = findViewById(R.id.red_wood_know_recycler);
        backImage = findViewById(R.id.back_button);
        backImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        getRedKnowInfo();
    }

    private void getRedKnowInfo() {
        RetrofitHelper.getNewsService()
                .getRedKnowInfo()
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<RedWoodInfoBean>>() {
                    @Override
                    public void onCompleted() {
                        RedKnowAdapter adapter = new RedKnowAdapter(dataList);
                        mRecyclerView.setAdapter(adapter);
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(List<RedWoodInfoBean> redWoodInfoBeans) {
                        dataList = redWoodInfoBeans;
                    }
                });
    }
}
